<?php

/**
 * 配置类
 */

namespace variant\core;

class Config {

    /**
     * 配置
     * @var array
     */
    static protected $config = array();

    static function init(){
        self::$config = array(
            'EXCEPTIONS'         => array(
                'DEBUG'              => true,                           //调试开关
                'DEBUG_LOG'          => true,                           //自定义调试开关
                'DEBUG_STRICT'       => false,                           //严格的错误检查
                'ENGINE'             => '',                             //自定义错误类
                'LOG_ON'             => false,
            ),
            'ROUTE'              => array(
                'REWRITE_ON'         => false,                           //伪静态开关
                'REWRITE_SUFFIX'     => '.html',                        //伪静态后缀
                'DEFAULT_APP'        => 'Index',                        //默认访问APP
                'DEFAULT_CONTROLLER' => 'Index',                        //默认访问控制器
                'DEFAULT_ACTION'     => 'index',                        //默认访问动作
            ),
            'SYSTEM'             => array(
                'DATA_PATH'          => BASE_PATH . 'data/',            //数据存储目录
                'TIMEZONE'           => 'PRC',                          //时区
            ),
            'TPL'=>array(
                'TPL_URL' => 'theme/',
                'TPL_THEME' => 'default',
                'TPL_SUFFIX' => '.html',
                'TPL_CACHE' => 'TPL_CACHE',
                'TPL_DEPR' => '_',
            ),
            'CACHE'=>array(
                'DEFAULT' => array(
                    'CACHE_TYPE' => 'FileCache',
                    'CACHE_PATH' => BASE_PATH . 'cache',
                    'GROUP' => 'default',
                    'HASH_DEEP' => 0,
                ),
                'TPL_CACHE' => array(
                    'CACHE_TYPE' => 'FileCache',
                    'CACHE_PATH' => BASE_PATH . 'cache',
                    'GROUP' => 'tpl',
                    'HASH_DEEP' => 0,
                ),

                'DB_CACHE' => array(
                    'CACHE_TYPE' => 'FileCache',
                    'CACHE_PATH' => BASE_PATH . 'cache',
                    'GROUP' => 'db',
                    'HASH_DEEP' => 2,
                ),
            ),
            'STORAGE'=>array(
                'default' => array('STORAGE_TYPE'=>'File'),
                'file' => array('STORAGE_TYPE'=>'File'),
            ),
        );
    }

    /**
     * 加载配置
     * @param  string $file 配置路径
     */
    static public function load($file) {
        if( !file_exists($file) ){
            throw new \Exception("Config file '{$file}' not found", 500); 
        }
        $config = require_once($file);
        foreach($config as $k=>$v){
            self::set($k, $v);
        }
    }

    /**
     * 读取配置
     * @param string $key
     */
    static public function get($key = '') {
        if( empty($key) ) return self::$config;
        if (!strpos($key, '.')) return self::$config[$key];
        $parts  = explode('.', $key);
        $return = self::$config;
        foreach ($parts as $part) {
            if(!isset($return[$part])) {
                return $default;
            } else {
                $return = $return[$part];
            }
        }
        return $return;
    }

    /**
     * 写入配置
     * @param string $key   配置名
     * @param mixed  $value 配置值
     */
    static public function set($key, $value) {
        if(is_array($value)){
            self::$config[$key] = array_merge((array)self::$config[$key], (array)$value);
        }else{
            self::$config[$key] = $value;
        }
    }
}
